package com.aki.ai_property.service.impl;

import com.aki.ai_property.dao.RepairsDao;
import com.aki.ai_property.dto.RepairsTjDto;
import com.aki.ai_property.entity.Repairs;
import com.aki.ai_property.service.intf.RepairsService;
import com.aki.ai_property.util.StringUtil;
import com.aki.ai_property.vo.PageBean;
import com.aki.ai_property.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author 孙贺鹏
 * @date 2022/8/28
 */
@Service
public class RepairsServiceImpl implements RepairsService {
    @Autowired
    private RepairsDao dao;
    @Override
    public Result save(Repairs repairs) {
        if (dao.save(repairs)>0){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @Override
    public Result update(Repairs repairs) {
        if (dao.update(repairs)>0){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @Override
    public Result delete(int id) {
        if(dao.delete(id)>0){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @Override
    public PageBean selectPage(String repairsContent, int page, int limit) {
        if(StringUtil.isNotEmpty(repairsContent)){
            repairsContent = "%"+repairsContent+"%";
        }
        int start =(page-1)*limit;
        return PageBean.ok(dao.selectCount(repairsContent),dao.selectPage(repairsContent,page,limit));
    }

    @Override
    public Result delIds(int[] ids) {
        if (dao.delByIds(ids)>0){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @Override
    public Result selectRep() {
        //1.查询数据
        List<RepairsTjDto> list=dao.selectRep();
        //2.组装结果数据
        HashMap<String,List> map=new HashMap<>();
        List<String> state=new ArrayList<>();
        List<Integer> num=new ArrayList<>();
        list.forEach(s->{
            state.add(s.getMaintenance_state());
            num.add(s.getNum());
        });
        map.put("state",state);
        map.put("num",num);
        System.out.println(map);
        //3.返回结果
        return Result.ok(map);

    }
}

